package com.tengju.activity.application;

import com.tengju.activity.application.shared.ApplicationException;
import com.tengju.activity.application.shared.ApplicationExceptionCode;
import com.tengju.activity.domain.model.activity.*;
import com.tengju.activity.domain.model.user.UserIdCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ActivityService {

    @Autowired
    ActivitySignUpRepository activitySignUpRepository;
    @Autowired
    ActivityConfigRepository activityConfigRepository;

    public Boolean signUp(UserIdCode idCode, ActivityId activityId) {
        if (idCode == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA,"用户idCode为空");
        }
        if (activityId == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA,"活动id为空");
        }
        activitySignUpRepository.signUp(new ActivitySignUp(idCode,activityId));
        return true;
    }

    public Integer signUpUserNum(ActivityId activityId) {
        if (activityId == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA,"活动id为空");
        }
        return activitySignUpRepository.countByActivityId(activityId);
    }

    /**
     * 是否已报名
     */
    public Boolean hasSignUp(UserIdCode idCode, ActivityId activityId) {
        if (idCode == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA,"用户idCode为空");
        }
        if (activityId == null) {
            throw ApplicationException.error(ApplicationExceptionCode.INVALID_DATA,"活动id为空");
        }
        return activitySignUpRepository.hasSignUp(idCode,activityId);
    }
}
